jsMotif.comm.xhttp = (function(){
    
    var jsonParse = (window.JSON)? window.JSON.parse : jsMotif.comm.jsonParse;
    
    function createXmlHttpObject() {
        if(window.XMLHttpRequest){
            return new XMLHttpRequest();
        }else{
            return new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    function getJson (url, onComplete, params) {
        params = params || {};
        
        var xmlHttp = createXmlHttpObject(),
            headers = params.headers || {},
            timeout = params.timeout || 4000;
        
        xmlHttp.timeout = timeout;
        xmlHttp.open('GET', url);
        xmlHttp.setRequestHeader('Accept','application/json');
        xmlHttp.setRequestHeader('Content-Type','application/json');
        
        if(headers){
            for(var key in headers){
                if(headers.hasOwnProperty(key)){
                    xmlHttp.setRequestHeader(key,headers[key]);
                }
            }
        }
        
        xmlHttp.ontimeout = function(){
            onComplete({}, 'timeout');
        };
        
        xmlHttp.onreadystatechange = function(){
            if(xmlHttp.readyState === 4){
                var respObject = {},
                    status = xmlHttp.status,
                    respText = xmlHttp.responseText;
                
                if(status === 200){
                    respObject = jsonParse(respText);
                }
                
                onComplete(respObject, (status === 200)? 'ok' : 'error');
            }
        };
        
        xmlHttp.send();            
    }
    
    return {
        getJson: getJson
    };
    
}());
